package com.zy.design.dd;

import com.zy.design.dd.entity.ActionEnum;
import com.zy.design.dd.entity.CountData;
import com.zy.design.dd.entity.DataLine;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhaoyi
 * @date 2021/3/19 13:42
 */
public class TsUse {

    public static void main(String[] args) {
        //测试使用
        DataLine d1 = new DataLine();
        d1.setDriverId("01");
        d1.setStartTime(0L);
        d1.setEndTime(30L);
        d1.setAction(ActionEnum.EMPTY);
        d1.setInOutDriving(2);
        d1.setModel(1);
        d1.setByWay(false);

        DataLine d2 = new DataLine();
        d2.setDriverId("01");
        d2.setStartTime(30L);
        d2.setEndTime(60L);
        d2.setAction(ActionEnum.MEET);
        d2.setInOutDriving(2);
        d2.setModel(1);
        d2.setByWay(false);

        List<DataLine> ls = new ArrayList<>();
        ls.add(d1);
        ls.add(d2);

        List<CountData> tmp = new ArrayList<>();
        for (DataLine dd : ls) {
            CountData run = AppContext.getInstance().run(dd);
            tmp.add(run);
            System.out.println(run);
        }

        //假设这两天刚好是一分钟内的数据,
        //如果不是可以加一个聚合字段把同一分钟设置成一样
        System.out.println("###########################################");

        Map<String, CountData> collect = tmp.stream().collect(
                Collectors.groupingBy(
                        CountData::getDriverId,
                        Collectors.reducing(new CountData(), (left, right) -> {
                            Long ae = Optional.ofNullable(left.getEmptyTime()).orElse(0L);
                            Long be = Optional.ofNullable(right.getEmptyTime()).orElse(0L);
                            right.setEmptyTime(ae+be);

                            Long ac = Optional.ofNullable(left.getContinuedTime()).orElse(0L);
                            Long bc = Optional.ofNullable(right.getContinuedTime()).orElse(0L);
                            right.setContinuedTime(ac+bc);


                            return right;
                        })
                )
        );

        collect.values().forEach(System.out::println);


    }

}
